package com.glory.shsi1rpt

import com.ivan.utils.PathUtil

/**
 * @Program: shsi1rpt
 * @Description: 日报
 * @Author: Ivan
 */
class DailyQueryController {

    def dynamicQueryService

    def dailyQuery() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartEndTime(params.todayStart, params.todayEnd, params.selectTime, params.startTime, params.endTime).startTime
        params.endTime = DateUtil.getStartEndTime(params.todayStart, params.todayEnd, params.selectTime, params.startTime, params.endTime).endTime
        def unYieldDescList = []
        def unYieldCnt = 0
        def reportList = []
        def tableHeadList = []
        def totalCount = 0
        def sysQueryName
        def sysQueryPath = PathUtil.getPath(this, request)
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            params.SYS_QUERY_NAME = 'dailyQueryUnYieldDescList'
            unYieldDescList = dynamicQueryService.queryForList(params)
            for (int i = 0; i < unYieldDescList.size(); i++) {
                unYieldCnt++
            }
            params.SYS_QUERY_NAME = 'dailyQueryList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            reportList = TranposUtils.colToRow("UN_YIELD_DESC", "UN_YIELD_QTY", reportList)
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            totalCount = reportList.size()
        }
        [
                startTime      : params.startTime ?: '',
                endTime        : params.endTime ?: '',
                partName       : params.partName ?: '',
                lotId          : params.lotId ?: '',
                unYieldCnt     : unYieldCnt,
                unYieldDescList: unYieldDescList,
                tableHeadList  : tableHeadList,
                reportList     : reportList,
                totalCount     : totalCount,
                SYS_QUERY_PATH : sysQueryPath,
                SYS_QUERY_NAME : sysQueryName
        ]
    }

    def exportExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.startTime = params.startTime != null ? URLDecoder.decode(params.startTime, 'UTF-8') : params.startTime
        params.endTime = params.endTime != null ? URLDecoder.decode(params.endTime, 'UTF-8') : params.endTime
        def unYieldCnt = 0
        params.SYS_QUERY_NAME = 'dailyQueryUnYieldDescList'
        def unYieldDescList = dynamicQueryService.queryForList(params)
        for (int i = 0; i < unYieldDescList.size(); i++) {
            unYieldCnt++
        }
        params.SYS_QUERY_NAME = 'dailyQueryList'
        def reportList = dynamicQueryService.queryForList(params)
        reportList = TranposUtils.colToRow("UN_YIELD_DESC", "UN_YIELD_QTY", reportList)
        String filename = new String("dailyQueryList.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [unYieldCnt: unYieldCnt, unYieldDescList: unYieldDescList, reportList: reportList])
    }
}